package cn.com.yiqi.wsc.portal.controller.lottery;

import cn.com.yiqi.wsc.Ro.AwardVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.lottery.criteria.LotteryActivityCriteria;
import cn.com.yiqi.wsc.entity.lottery.*;
import cn.com.yiqi.wsc.service.lottery.LotteryActivityService;
import cn.com.yiqi.wsc.service.user.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 何刚
 * @version V1.0
 * @Title: LotteryActivityController
 * @Package cn.com.yiqi.wsc.portal.controller.lottery
 * @Description: 抽奖活动管理
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @date 2017/3/22
 */
@RequestMapping("/lottery")
@Controller
public class LotteryActivityController {

    @Autowired
    private LotteryActivityService lotteryActivityService;
    @Autowired
    private UserService userService;


    @RequestMapping("/list")
    @ResponseBody
    public Result pageList(@RequestBody LotteryActivityCriteria criteria) {
        Result result = new Result();

        result.setStatus(ResultStatusEnum.操作成功.getCode());

        Result<PageInfo<LotteryActivity>> infoResult = lotteryActivityService.pageList(criteria);

        PageInfo pageInfo = infoResult.getReturnValue();

        result.putData("list", pageInfo.getList());
        result.putData("total", pageInfo.getTotal());

        return result;

    }


    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody LotteryActivityDTO activityDTO) {
        Result result = new Result();
        if(StringUtils.isEmpty(activityDTO.getName())){
            return ResultUtil.paramError(result,"活动名称不能为空！");

        }
        if(activityDTO.getStartTime()==null){
            return ResultUtil.paramError(result,"开始时间不能为空！");

        }
        if(activityDTO.getEndTime()==null){
            return ResultUtil.paramError(result,"结束时间不能为空！");

        }
        if(activityDTO.getFreeTime()==null){
            return ResultUtil.paramError(result,"免费次数不能为空！");

        }


/*        if(StringUtils.isEmpty(activityDTO.getPointBuy())){
            return ResultUtil.paramError(result,"积分兑换不能为空！");

        }*/


//        if(StringUtils.isEmpty(activityDTO.getLotteryRules())){
//            return ResultUtil.paramError(result,"活动说明不能为空！");
//
//        }
        if(StringUtils.isEmpty(activityDTO.getBanner())){
            return ResultUtil.paramError(result,"活动图片不能为空！");

        }


        List<LotteryItemsDTO> itemsDTOs = activityDTO.getItemsList();

        if (itemsDTOs == null || itemsDTOs.isEmpty()) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("请添加奖品");
            return result;

        }

        if (itemsDTOs.size() != 8) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("添加奖品数量必须为8");
            return result;
        }

        for(LotteryItemsDTO lotteryItemsDTO : itemsDTOs){
            if(StringUtils.isEmpty(lotteryItemsDTO.getName())){
                return ResultUtil.paramError(result,"奖品名称不能为空！");
            }
            if(StringUtils.isEmpty(lotteryItemsDTO.getPicUrl())){
                return ResultUtil.paramError(result,"中奖图片不能为空！");
            }
            if(lotteryItemsDTO.getWinRate()==null){
                return ResultUtil.paramError(result,"中奖率不能为空！");
            }
            if(lotteryItemsDTO.getType()==null){
                return ResultUtil.paramError(result,"奖品类型不能为空！");
            }
            if(lotteryItemsDTO.getContent()==null){
                return ResultUtil.paramError(result,"奖品不能为空！");
            }
        }

        result.setStatus(ResultStatusEnum.操作成功.getCode());
        lotteryActivityService.insert(activityDTO, userService.getCurrentUserId());

        return result;


    }


    @RequestMapping("/record/insert")
    @ResponseBody
    public Result insertRecord(@RequestBody LotteryRecord lotteryRecord) {
        Result result = new Result();
//        if(lotteryRecord.getCustomerId()==null){
//            return  ResultUtil.paramError(result, "客户名不能为空");
//        }
        if(lotteryRecord.getLotteryItemId()==null){
            return  ResultUtil.paramError(result, "奖项不能为空");
        }
        if(lotteryRecord.getActivityId()==null){
            return  ResultUtil.paramError(result, "活动不能为空");
        }
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        lotteryActivityService.insertRecord(lotteryRecord, userService.getCurrentUserId());

        return result;


    }


    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody LotteryActivityDTO activityDTO) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        List<LotteryItemsDTO> itemsDTOs = activityDTO.getItemsList();


        if (itemsDTOs == null || itemsDTOs.isEmpty()) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("请添加奖品");
            return result;

        }

        if (itemsDTOs.size() != 8) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("添加奖品数量必须为8");
            return result;
        }

        lotteryActivityService.update(activityDTO, userService.getCurrentUserId());

        return result;

    }


    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody LotteryActivityCriteria criteria) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        lotteryActivityService.delete(criteria);


        return result;

    }

    @RequestMapping("/deleteRecord")
    @ResponseBody
    public Result delete(HttpServletRequest request) {
        Result result = new Result();

        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        result = lotteryActivityService.deleteRecord(Long.parseLong(id));

        return result;

    }


    @RequestMapping("/enable")
    @ResponseBody
    public Result enable(@RequestBody LotteryActivityCriteria criteria) {
        Result result = new Result();

        LotteryActivityCriteria pageCriteria = new LotteryActivityCriteria();
        pageCriteria.setCurrentPage(1);
        pageCriteria.setPageSize(1);
        pageCriteria.setEnable(1);
        Result<PageInfo<LotteryActivity>> pageResult = lotteryActivityService.pageList(pageCriteria);
        if (200 != pageResult.getStatus() || null == pageResult.getReturnValue()) {
            result.setStatus(ResultStatusEnum.服务器内部错误.getCode());
            return result;
        }
        if (pageResult.getReturnValue().getTotal() >= 1) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("当前已经有抽奖活动启用了，请禁用该活动然后再启用本活动！");
            return result;
        }

        result.setStatus(ResultStatusEnum.操作成功.getCode());

        lotteryActivityService.enable(criteria);


        return result;

    }

    @RequestMapping("/disable")
    @ResponseBody
    public Result disable(@RequestBody LotteryActivityCriteria criteria) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        lotteryActivityService.disable(criteria);

        return result;

    }

    @RequestMapping("/detail")
    @ResponseBody
    public Result detail(@RequestBody LotteryActivityCriteria criteria) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        if (criteria.getId() == null) {
            return ResultUtil.paramError(result, "参数错误");
        }

        result.putData("detail", lotteryActivityService.detail(criteria));

        return result;

    }


    @RequestMapping("/record/list")
    @ResponseBody
    public Result recordList(@RequestBody LotteryActivityCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        PageInfo<AwardVO> info = lotteryActivityService.selectLotteryRecordByActivityId(criteria).getReturnValue();

        result.putData("list", info.getList());

        result.putData("total", info.getTotal());

        return result;

    }

}
